<template>
  <div>
    <router-link tag="div" to="/" class="header-abs">
      <span class="iconfont">&#xe61c;</span>
    </router-link>
    <div class="header-fixed" v-show="showAbs" :style="opacityStyle">
      <router-link to="/" class="back-link">
        <span class="iconfont header-back">&#xe61c;</span>
      </router-link>
      <h1 class="header-title">{{sightName}}</h1>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    sightName: String
  },
  data () {
    return {
      showAbs: false,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = true
      } else {
        this.showAbs = false
      }
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
 @import '~@/assets/css/varibles.styl'
  .header-abs
    position absolute
    top .2rem
    left .2rem
    width .6rem
    height .6rem
    line-height .6rem
    text-align center
    border-radius .6rem
    background rgba(0, 0, 0, .6)
    color #fff
    span
      display block
      width 100%
      height 100%
  .header-fixed
    width 100%
    height .8rem
    line-height .8rem
    display flex
    align-items center
    background $bgColor
    position fixed
    top 0
    left 0
    right 0
    .back-link
      z-index 999
      .header-back
        display block
        width .8rem
        text-align center
        color #fff
    .header-title
      margin-left -0.8rem
      flex 1
      font-size .32rem
      color #fff
      text-align center
</style>
